package com.yizisu.playerlibrary.view.video_view.view

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.ProgressBar
import androidx.appcompat.widget.AppCompatTextView
import com.yizisu.playerlibrary.view.video_view.helper.dip


/**
 * 中间提示弹窗
 */
open class PlayerTipsView : LinearLayout {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    private val titleView by lazy {
        AppCompatTextView(context).apply {
            gravity = Gravity.CENTER
            textSize = 13f
            setTextColor(Color.WHITE)
        }
    }
    private val desView by lazy {
        AppCompatTextView(context).apply {
            gravity = Gravity.CENTER
            textSize = 11f
            setTextColor(Color.WHITE)
        }
    }
    private val progressView by lazy {
        ProgressBar(
            context,
            null,
            android.R.attr.progressBarStyleHorizontal,
        ).apply {
            max = 10_000_000
        }
    }

    init {
        orientation = VERTICAL
        gravity = Gravity.CENTER
        background = GradientDrawable().also {
            it.color = ColorStateList.valueOf(Color.parseColor("#66444444"))
            it.cornerRadius = dip(8f)
        }
        setPadding(dip(16), dip(12), dip(16), dip(8))
        addView(
            titleView, LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT
            )
        )

        addView(
            desView, LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT
            ).also {
                it.topMargin = dip(8)
            }
        )

        addView(
            progressView, LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT
            ).also {
                it.topMargin = dip(8)
            }
        )
    }

    fun setTips(title: String?, des: String?, progress: Float?/*0,1*/) {
        if (title.isNullOrBlank()) {
            titleView.visibility = View.GONE
        } else {
            titleView.visibility = View.VISIBLE
            titleView.text = title
        }
        if (des.isNullOrBlank()) {
            desView.visibility = View.GONE
        } else {
            desView.visibility = View.VISIBLE
            desView.text = des
        }
        if (progress != null) {
            progressView.visibility = View.VISIBLE
            progressView.progress = (progressView.max * progress).toInt()
        } else {
            progressView.visibility = View.GONE
        }
    }
}